import { Component, ElementRef, OnInit, Output, Input, ViewChild, AfterViewInit, EventEmitter } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';

import { RouterModule, Router, ActivatedRoute } from '@angular/router';
import { Service } from '../../../server/server';
import { RolesDetailsComponent } from '../roles/../roles-details/roles-details.component';
import { Pagination, PaginationUrl } from '../../../public/serve/pcrudInfo';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
declare var $: any;


@Component({
  selector: 'app-roles-table',
  templateUrl: './roles-table.component.html',
})
export class RolesTableComponent implements OnInit {

  @Input() tableNumber: number;

  @Input() searchData: any;

  right: any = {
    "roles": {},
  };
  constructor(
    private http: HttpClient, 
    private server: Service, 
    private router: Router, 
    private tableListService: CudTableService, 
    private removeTableService: RemoveTableService,
    private hintsService:HintsService
    ) {
    this.right = $.extend(this.right, server.getRight());
  }
  public userId: any;
  public rolesData: string[];
  public rolesDetailsData = [];

  ngOnInit() {
    //console.log(this.userId)
    this.userId = this.server.getuser();
    this.getTabList(1, 20, '0', this.searchData);
  }

  @Output()
  tabeListOutput: EventEmitter<string> = new EventEmitter();

  public tabVal: string[];
  // 表格
  getTabList(pageNumber, pageSize, id, queryCriteria) {

    let url = this.server.baseUrl+'admin/roleGroups/getRoleGroupsList';
    let data = { pageNumber: pageNumber, pageSize: pageSize, id: id, queryCriteria: queryCriteria }

    this.tableListService.postRequest(url, data).then((data: any) => {
      if (data.result_code == '0000') {
        this.tabVal = data.result;
        this.tabeListOutput.emit(data.length);
      }
      // console.log(data);
    }).catch();
  }


  @Output()
  detailsOutput: EventEmitter<rolesDetailsDatatype> = new EventEmitter();

  // 详情（弹窗），用于展示默认数据
  detailsPopup(tabval) {
    $("#details").modal("show");
    this.outputTabVal(tabval);
    //console.log(tabval)
  }

  // 修改（弹窗），用于展示默认数据
  editPopup(tabval) {
    this.outputTabVal(tabval);
  }

  // 每一行的表格信息
  outputTabVal(tabval) {
    let tabVal: rolesDetailsDatatype = {
      roleGroupName: tabval.roleGroupName,
      roleGroupId: tabval.roleGroupId,
      createDate: tabval.createDate,
      description: tabval.description,
      lastUpdatedDate: tabval.lastUpdatedDate,
      lastUpdatedUserId: tabval.lastUpdatedUserId,
      createUserId: tabval.createUserId,

    };
    this.detailsOutput.emit(tabVal);
  }
  public rolesselectId = { roleGroupId: '', roleGroupName: '', description: '', lastUpdatedUserId: '' };
  public body = { roleGroupId: '', roleGroupName: '', description: '', lastUpdatedUserId: '' };
  //focus
  roles_focus(rolesdata) {
    //console.log("用户输入")
    this.rolesselectId = rolesdata;
    this.body = {
      roleGroupId: this.rolesselectId.roleGroupId,
      roleGroupName: this.rolesselectId.roleGroupName,
      description: this.rolesselectId.description,
      lastUpdatedUserId: this.userId
    };
    //console.log(this.body)
    //return this.rolesselectId;
  }

  //blur
  //验证字符是否通过
  public checktext: any;
  roles_blur() {
    //console.log("失去焦点")
    //console.log(this.body)
    var check = /^[a-zA-Z\d\u4E00-\u9FA5_]+$/;
    if (check.test(this.body.roleGroupName) || this.body.roleGroupName == '') {
      this.checktext = true;
    } else {
      this.checktext = false;
    }
    let url = this.server.baseUrl+'admin/roleGroups/modify';
    this.tableListService.postRequest(url, this.body).then((res: any) => {
      //console.log(res.result_code)
      if (this.body.roleGroupId != '' && this.body.roleGroupName != '' && this.checktext) {
        let url = this.server.baseUrl+'admin/roleGroups/modify';
        this.tableListService.postRequest(url, this.body).then((res: any) => {
          //res = res.json();
          if (res.result_code == '0000') {
           // console.log(res)

            this.body = { roleGroupId: '', roleGroupName: '', description: '', lastUpdatedUserId: this.userId };
            this.hintsService.messages(res.message,res.result_code);
            this.getTabList(1, 20, 0, this.searchData);
            //this.roleslist();
            if (res.message !== '修改角色组成功！') {
              this.hintsService.messages(res.message,res.result_code);
              this.getTabList(1, 20, 0, this.searchData);
            }
            //console.log( this.server.prompt)
          } else {
            this.hintsService.messages(res.message,res.result_code);
            this.getTabList(1, 20, 0, this.searchData);
          }
        });
      } else {
        if (this.inputnull == '' || this.checktext == false) {
          //console.log("提交数据为空...");
          this.hintsService.messages(res.message,res.result_code);
          this.getTabList(1, 20, 0, this.searchData);
        }
      }
    }).catch();
  }
  //input null
  public inputnull: String;
  checkname(event) {
    this.inputnull = event;
   // console.log(this.inputnull)
  }

  // 当前页（默认为第一页）
  public pageNumber: number = 1;
  // 每页显示多少条数据（默认每页显示20条）
  public pageSize: number = 20;
  // id
  public id: string = '0';

  //#delete

  // 全选
  public isSelectAll: boolean = false;
  public isChose: boolean = false;
  isSelect: boolean = true;
  isDisable: any;              // 按钮（启用/禁用）

  @Output() isSelectFromTable = new EventEmitter<boolean>();
 
  selectAll(bool, tabVal) {
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'roleGroupId');
    this.isSelect = !this.isSelectAll;
    this.isSelectFromTable.emit(this.isSelect);
  }

  // 单选（多选）
  chose(bool, roleGroupId, length) {
    this.isDisable = this.removeTableService.removeChose(bool, roleGroupId, length);
    this.isSelectAll = this.isDisable.bool;
    this.isDisable.len ? this.isSelect = false : this.isSelect = true;
    this.isSelectFromTable.emit(this.isSelect);
  }

  // 没有数据时取消勾选全选按钮
  cancelSelectAll() {
    this.isSelectAll = false;
    this.isChose = false;
  }

}

export class rolesDetailsDatatype {
  public createUserId: string;
  public description: string;
  public lastUpdatedDate: string;
  public lastUpdatedUserId: string;
  public roleGroupId: string;
  public roleGroupName: string;
  public createDate: string;
}